package com.ddd.demo.listviewdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by doushuqi on 15/4/29.
 */
public class BaseAdapterFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        final ListView listView= (ListView) view.findViewById(R.id.list_container);
        List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 20; i++) {
            HashMap<String, Object> content = new HashMap<>();
            content.put("title", "BaseAdapter title");
            content.put("number", i);
            content.put("image", R.drawable.ic_launcher);
            list.add(content);
        }
        listView.setAdapter(new MyBaseAdapter(getActivity(), list));
        return view;
    }

    private class MyBaseAdapter extends BaseAdapter {
        Context mContext;
        List<HashMap<String, Object>> mItem;
        public MyBaseAdapter(Context context, List<HashMap<String, Object>> item) {
            mContext = context;
            mItem = item;
        }

        @Override
        public int getCount() {
            return mItem.size();
        }

        @Override
        public Object getItem(int position) {
            return mItem.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.number = (TextView) convertView.findViewById(R.id.number);
                viewHolder.photo = (ImageView) convertView.findViewById(R.id.iv);

                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.title.setText(mItem.get(position).get("title").toString());
            viewHolder.number.setText(mItem.get(position).get("number").toString());
            viewHolder.photo.setImageResource(Integer.parseInt(mItem.get(position).get("image").toString()));

            return convertView;
        }
    }

    final class ViewHolder{
        TextView title;
        TextView number;
        ImageView photo;
    }
}
